package com.slipper.system.codeless.controller;

import com.slipper.core.gson.GsonUtil;
import com.slipper.system.affix.service.AffixItemService;
import com.slipper.system.codeless.bo.*;
import com.slipper.system.codeless.service.MobilePageService;
import com.slipper.system.codeless.vo.MobileVO;
import com.slipper.util.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.service.MobileService;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@RestController
@RequestMapping("/system/mobile")
public class MobileController {
    @Resource
    private MobileService mobileService;
    @Resource
    private MobilePageService mobilePageService;
    @Resource
    private AffixItemService affixItemService;

    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody MobileBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(mobileService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody MobileBO bo) {
        return ResultUtil.successResult(mobileService.queryList(bo));
    }

    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody MobileBO bo) {
        return ResultUtil.successResult(mobileService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody MobileBO bo) {
        return ResultUtil.saveResult(mobileService.save(bo), bo);
    }

    @PostMapping(value = "/copy")
    public String copy(@RequestBody MobileBO bo) {
        if (mobileService.copy(bo) > 0) {
            return ResultUtil.successResult("复制成功！");
        } else {
            return ResultUtil.failureResult("复制失败！");
        }
    }

    @PostMapping(value = "/updateHome")
    public String updateHome(@RequestBody MobileBO bo) {
        return ResultUtil.operateResult(mobileService.updateHome(bo), "设为首页");
    }

    @PostMapping(value = "/updateLogin")
    public String updateLogin(@RequestBody MobileBO bo) {
        return ResultUtil.operateResult(mobileService.updateLogin(bo), "设为登录页");
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody MobileBO bo) {
        return ResultUtil.deleteResult(mobileService.delete(bo));
    }

    @PostMapping(value = "/exportData")
    public void exportData(HttpServletResponse response, @RequestBody MobileBO bo) {
        MobileVO vo = new MobileVO();
        vo.setMobileList(mobileService.queryList(bo));
        vo.getMobileList().forEach(mobileBO -> {
            MobilePageBO mobilePageQO = new MobilePageBO();
            mobilePageQO.setMobileId(mobileBO.getId());
            vo.addMobilePageList(mobilePageService.queryList(mobilePageQO));
        });
        vo.getMobilePageList().forEach(item -> vo.getFileList().addAll(affixItemService.queryWithFile(Visual.parseFileId(item.getJsonText()))));
        String fileName = "app-" + DateUtil.format(new Date(), "dd-HH-mm-ss") + ".dat";
        FileUtil.downloadContent(response, fileName, AESUtil.encrypt(GsonUtil.toJson(vo)));
    }

    @PostMapping(value = "/importData")
    public String importData(MobileBO bo) {
        if (bo.getFile() == null) {
            return ResultUtil.failureResult("请选择上传文件！");
        }
        try {
            String content = new String(bo.getFile().getBytes(), StandardCharsets.UTF_8);
            MobileVO vo = GsonUtil.fromJson(AESUtil.decrypt(content), MobileVO.class);
            mobileService.saveList(vo.getMobileList());
            mobilePageService.saveList(vo.getMobilePageList());
            affixItemService.saveList(vo.getFileList());
            return ResultUtil.successResult("导入应用成功。");
        } catch (Exception e) {
            return ResultUtil.failureResult("文件解释出错！");
        }
    }
}
